# coding:utf8

from com.power.auto.test.enums.enmus import *
import os
from jpype import *
from com.power.auto.test.utils.path_utils import *

from com.power.auto.test.utils.yaml_util import *


class EnvManager(object):
    _env = ENV.QA

    _maven_init = MavenInitConfig.OPEN

    _root_path = ''

    _config_path = ''

    _lib_path = ''

    _inited = False

    @staticmethod
    def init():
        root_path = get_root_path()
        print(root_path)
        # 关闭遗留vm
        if isJVMStarted():
            print('已启动vm')
            shutdownJVM()
        EnvManager._root_path = root_path
        EnvManager._config_path = root_path + '/config/'
        EnvManager._lib_path = root_path + '/lib/'
        print(EnvManager._config_path)
        yaml_path = os.path.join(EnvManager._config_path + 'env.yml')
        f = open(yaml_path, 'r', encoding='utf-8')
        cfg = f.read()
        env_dict = yaml.load(cfg)  # 用load方法转字典
        if env_dict['env'] == ENV.INNER.name:
            EnvManager._env = ENV.INNER.name
        elif env_dict['env'] == ENV.QA.name:
            EnvManager._env = ENV.QA.name
        else:
            EnvManager._env = ENV.PROD.name
        if env_dict['init_manven'] == MavenInitConfig.OPEN.name:
            EnvManager._maven_init = MavenInitConfig.OPEN.name
        else:
            EnvManager._maven_init = MavenInitConfig.CLOSE.name
        EnvManager._inited = True

    @staticmethod
    def get_env():
        if EnvManager._inited is False:
            EnvManager.init()
        return EnvManager._env

    @staticmethod
    def get_maven_init():
        if EnvManager._inited is False:
            EnvManager.init()
        return EnvManager._maven_init

    @staticmethod
    def get_config_path():
        if EnvManager._inited is False:
            EnvManager.init()
        return EnvManager._config_path

    @staticmethod
    def get_lib_path():
        if EnvManager._inited is False:
            EnvManager.init()
        return EnvManager._lib_path

    @staticmethod
    def destory():
        EnvManager._inited = False
        if isJVMStarted():
            shutdownJVM()


if __name__ == '__main__':
    dd = EnvManager.init()
    print(EnvManager.get_env())
    print(EnvManager.get_maven_init())
    print(EnvManager.get_lib_path())
    print(EnvManager.get_config_path())
